package com.kuang.dao.user;

import com.kuang.dao.BaseDao;
import com.kuang.pojo.Role;
import com.kuang.pojo.User;
import com.mysql.cj.util.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao{
//    public User user=null;

//    public UserDaoImpl() {
//        UserDao userDao = new UserDaoImpl();
//    }

//    登录
    public User login(Connection connection, String userCode) {

        String sql = "select * from smbms_user where userCode=?";
        Object[] param={userCode};
        ResultSet resultSet=null;
        PreparedStatement preparedStatement =null;
        User user=null;
        if (connection != null) {
            resultSet = BaseDao.execute(connection, sql, param, resultSet, preparedStatement);
//                id           bigint auto_increment comment 'id' primary key,
//                userCode     varchar(15) null comment '用户编码',
//                userName     varchar(15) null comment '用户名字',
//                userPassword varchar(20) null comment '用户密码',
//                gender       int         null comment '性别',
//                birthday     date        null comment '出生日期',
//                phone        varchar(20) null comment '电话',
//                address      varchar(30) null comment '地址',
//                userRole     bigint      null comment '用户角色',
//                createdBy    bigint      null comment '创建者',
//                creationDate datetime    null comment '创建时间',
//                modifyBy     bigint      null comment '更新者',
//                modifyDate   datetime    null comment '更新时间'
            try {
                if (resultSet.next()) {
                    user = new User();
                    user.setUserId(resultSet.getInt("id"));
                    user.setUserCode(resultSet.getString("userCode"));
                    user.setUserName(resultSet.getString("userName"));
                    user.setUserPassword(resultSet.getString("userPassword"));
                    user.setGender(resultSet.getInt("gender"));
                    user.setBirthday(resultSet.getDate("birthday"));
                    user.setPhone(resultSet.getString("phone"));
                    user.setAddress(resultSet.getString("address"));
                    user.setUserRole(resultSet.getInt("userRole"));
                    user.setCreatedBy(resultSet.getInt("createdBy"));
                    user.setCreationDate(resultSet.getDate("creationDate"));
                    user.setModifyBy(resultSet.getInt("modifyBy"));
                    user.setModifyDate(resultSet.getDate("modifyDate"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                BaseDao.connectClose(connection, preparedStatement, resultSet);
            }
        }


        return user;
    }

//    修改密码
    public int updatePassword(Connection connection, int id, String newPassword) {
        PreparedStatement preparedStatement = null;
        String sql = "update  smbms_user set userPassword = ? where id= ?";
        Object[] param = {newPassword, id};
        int updateRows = 0;
        if(connection != null){
             updateRows=BaseDao.execute(connection, sql, param, preparedStatement);
             BaseDao.connectClose(connection,preparedStatement,null);
        }
        return updateRows;
    }

    public List<User> getUserList(Connection connection,
                                  String userName,
                                  String roleCode,
                                  int pageSize,
                                  int currentPageNo) {
        List<User> userList = new ArrayList<User>();
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;


        StringBuffer sql=this.queryUser(userName,roleCode);

//
        sql.append(" limit "+currentPageNo+" , "+pageSize);

        System.out.println(sql.toString());
        Object[]  param = {};
        resultSet=BaseDao.execute(connection,sql.toString(),param,resultSet,preparedStatement);

            try {
                while (resultSet.next()) {
                    User user =new User();
                    Role role = new Role();
                    user.setUserId(resultSet.getInt("id"));
                    user.setUserCode(resultSet.getString("userCode"));
                    user.setUserName(resultSet.getString("userName"));
                    user.setGender(resultSet.getInt("gender"));
                    user.setAge(resultSet.getDate("birthday"));
                    user.setPhone(resultSet.getString("phone"));
                    user.setUserRole(resultSet.getInt("userRole"));

//                  加角色
                    role.setRoleName(resultSet.getString("roleName"));
                    user.setRole(role);
                    user.setUserRoleName(role);
                    userList.add(user);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                BaseDao.connectClose(connection,preparedStatement,resultSet);
            }

        return userList;
    }

    public List<User> getUserTotal(Connection connection, String userName, String roleCode) {
        List<User> userList = new ArrayList<User>();
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;

        StringBuffer sql = this.queryUser(userName,roleCode);

        Object[]  param = {};
        resultSet=BaseDao.execute(connection,sql.toString(),param,resultSet,preparedStatement);

        try {
            while (resultSet.next()) {
                User user =new User();
                Role role = new Role();
                user.setUserId(resultSet.getInt("id"));
                user.setUserCode(resultSet.getString("userCode"));
                user.setUserName(resultSet.getString("userName"));
                user.setGender(resultSet.getInt("gender"));
                user.setAge(resultSet.getDate("birthday"));
                user.setPhone(resultSet.getString("phone"));
                user.setUserRole(resultSet.getInt("userRole"));

//                  加角色
                role.setRoleName(resultSet.getString("roleName"));
                user.setRole(role);
                user.setUserRoleName(role);
                userList.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.connectClose(connection,preparedStatement,resultSet);
        }

        return userList;
    }

    public StringBuffer queryUser( String userName, String roleCode){
        StringBuffer sql = new StringBuffer();
        sql.append("select u.*,r.roleName from smbms_user u left join smbms_role r on u.userRole=r.roleCode");

        if(!StringUtils.isNullOrEmpty(userName)){
            userName = "'%"+userName+"%'";
            sql.append(" where u.userName like "+ userName);
            if(!StringUtils.isNullOrEmpty(roleCode)){
                sql.append(" and r.roleCode = ").append(roleCode);
            }
        }else {
            if(!StringUtils.isNullOrEmpty(roleCode)){
                sql.append(" where r.roleCode = ").append(roleCode);
            }
        }

        return sql;
    }
}
